package com.lanxin.oa.zu.controller;

import com.lanxin.bean.*;
import com.lanxin.oa.zu.service.KehuService;
import com.lanxin.util.PageBean;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("kehu")
public class KehuController {
    @Autowired
    private KehuService kehu;
    @RequestMapping("selectcp")
    public ModelAndView selectcp(Integer page, Integer pageSize, String linkname){
        PageBean list=kehu.selectcp(page,pageSize,linkname);
        ModelAndView mv=new ModelAndView();
        mv.addObject("list",list);
        mv.setViewName("../view/zu/chanpinlist.jsp");
        return mv;
    }


    @RequestMapping("toAddandUpdate")
    public ModelAndView selectcp(Integer oid){
        ModelAndView mv=new ModelAndView();
        if(oid!=null){
         KhProduct cp= kehu.selectcpByid(oid);
         mv.addObject("cp",cp);
        }
        mv.setViewName("../view/zu/addcp.jsp");
        return mv;
    }
    @RequestMapping("addcp")
    public ModelAndView insertcp(KhProduct cp ,HttpServletRequest request, MultipartFile imgfile){
        kehu.insertcp(cp,request,imgfile);
        ModelAndView mv=new ModelAndView();
        mv.setViewName("redirect:selectcp");
        return mv;
    }
    @RequestMapping("byname")
    @ResponseBody
    public Map<String,Boolean> checktype(String linkname,Integer oid){
        Map<String,Boolean> map=new HashMap<String, Boolean>();
        boolean b=kehu.selectcpByName(linkname,oid);
        if(!b){
            map.put("valid", true);
            return map;
        }
        map.put("valid", false);
        return map;
    }

    @RequestMapping("deletecp")
    @ResponseBody
    public boolean deletezu(KhProduct cp){
        return kehu.deletecp(cp);
    }
///////////////////////////客户//////////////////////////////////////////////////////////////////
    @RequestMapping("selectkh")
    public ModelAndView selectcp(Integer page, Integer pageSize, String customerName,String phone,Integer companyType,Integer vocation){
        PageBean list=kehu.selectkh(page,pageSize,customerName,phone,companyType,vocation);
        List hangye=kehu.selecthy();
        List xingzhi=kehu.selectxz();
        ModelAndView mv=new ModelAndView();
        mv.addObject("list",list);
        mv.addObject("hangye",hangye);
        mv.addObject("xingzhi",xingzhi);
        mv.setViewName("../view/zu/kehulist.jsp");
        return mv;
    }
    @RequestMapping("toAddkh")
    public ModelAndView selectkh(){
        List xinyong=kehu.selectxy();
        List hangye=kehu.selecthy();
        List xingzhi=kehu.selectxz();
        ModelAndView mv=new ModelAndView();
        mv.addObject("xinyong",xinyong);
        mv.addObject("hangye",hangye);
        mv.addObject("xingzhi",xingzhi);
        mv.setViewName("../view/zu/addkehu.jsp");
        return mv;
    }
    @RequestMapping("addkehu")
    public ModelAndView addkehu(KhCustomer kh, String[] contactName, String[] job, String[] cphone, String[] cemail, String[] czipcode, String[] caddress ){
        kehu.insertkehu(kh,contactName,job,cphone,cemail,czipcode,caddress);
        ModelAndView mv=new ModelAndView();
        mv.setViewName("redirect:selectkh");
        return mv;
    }
    @RequestMapping("toUpdatekh")
    public ModelAndView toupdatekh(Integer oid){
        KhCustomer kh=kehu.selectkhById(oid);
        List xinyong=kehu.selectxy();
        List hangye=kehu.selecthy();
        List xingzhi=kehu.selectxz();
        ModelAndView mv=new ModelAndView();
        mv.addObject("kh",kh);
        mv.addObject("xinyong",xinyong);
        mv.addObject("hangye",hangye);
        mv.addObject("xingzhi",xingzhi);
        mv.setViewName("../view/zu/updatekehu.jsp");
        return mv;
    }
    @RequestMapping("Updatekh")
    public ModelAndView updatekh(KhCustomer kh, Integer[] coid, String[] contactName, String[] job, String[] cphone, String[] cemail, String[] czipcode, String[] caddress){
        kehu.updatekehu(kh,coid,contactName,job,cphone,cemail,czipcode,caddress);
        ModelAndView mv=new ModelAndView();
        mv.setViewName("redirect:selectkh");
        return mv;
    }

    @RequestMapping("xiangqing")
    public ModelAndView xiangqing(Integer oid){
        KhCustomer kh=kehu.selectkhById(oid);
        ModelAndView mv=new ModelAndView();
        mv.addObject("kh",kh);
        mv.setViewName("../view/zu/kehuxiangqing.jsp");
        return mv;
    }
    @RequestMapping("guanxin")
    public ModelAndView xiangqing(Integer customerid,Integer page,Integer pageSize,String khname){
        PageBean list=kehu.selectkhgxcp(customerid,page,pageSize);
        List cplist=kehu.selectcp();
        List lxrlist=kehu.selectlxr(customerid);
        ModelAndView mv=new ModelAndView();
        mv.addObject("list",list);
        mv.addObject("customerid",customerid);
        mv.addObject("cplist",cplist);
        mv.addObject("lxrlist",lxrlist);
        mv.addObject("khname",khname);
        mv.addObject("customerid",customerid);
        mv.setViewName("../view/zu/kehuguanxin.jsp");
        return mv;
    }
    @RequestMapping("insertgx")
    public ModelAndView insertgx(KhCustomerfavorite gx){
        kehu.insertgxcp(gx);
        ModelAndView mv=new ModelAndView();
        mv.setViewName("redirect:guanxin");
        return mv;
    }

    @RequestMapping("yngx")
    @ResponseBody
    public Map<String,Boolean> yngx(Integer  customerid,Integer productid){
        Map<String,Boolean> map=new HashMap<String, Boolean>();
        boolean b=kehu.guanxin(customerid,productid);
        if(!b){
            map.put("valid", true);
            return map;
        }
        map.put("valid", false);
        return map;
    }

    @RequestMapping("updateguanxin")
    public ModelAndView updateganxin(KhCustomerfavorite gx){
                kehu.updateguanxin(gx);
               ModelAndView mv=new ModelAndView();
               mv.setViewName("redirect:guanxin");
               return mv;
    }

    @RequestMapping("insertgz")
    public ModelAndView insertgz(KhContactrecord gz){
        kehu.insertgenzong(gz);
        ModelAndView mv=new ModelAndView();
        mv.setViewName("redirect:guanxin");
        return mv;
    }
    @RequestMapping("genzongjilu")
    public ModelAndView genzongjilu(Integer  favoriteid,Integer page,Integer pageSize){
        PageBean list=kehu.selectgzjl(favoriteid,page,pageSize);
        ModelAndView mv=new ModelAndView();
        mv.addObject("list",list);
        mv.addObject("favoriteid",favoriteid);
        mv.setViewName("../view/zu/genzongjilu.jsp");
        return mv;
    }


}
